package org.dut.community.entity;

import lombok.Getter;

@Getter
public class Page {
    // 当前页面
    private int current = 1;
    //每页上限
    private int limit = 10;
    //数据总数
    private int rows;
    // 查询路径
    private String path;

    public void setCurrent(int current) {
        if (current >= 1) {
            this.current = current;
        }
    }
    public void setLimit(int limit) {
        if (limit >= 1 && limit <= 100) {
            this.limit = limit;
        }
    }
    public void setRows(int rows) {
        if(rows >= 0){
            this.rows = rows;
        }
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 计算总页数
     */
    public int getTotal() {
        if(rows % limit == 0){
            return rows / limit;
        }else{
            return rows / limit + 1;
        }
    }

    /**
     * 计算起始页码
     */
    public int getFrom(){
        int from = current-2;
        return Math.max(from,1);
    }

    /**
     * 计算结束页码
     */
    public int getTo(){
        int to = current+2;
        return Math.min(to,getTotal());
    }

    /**
     * 获取分页的起始行
     */
    public int getOffset(){
        return (current-1)*limit;
    }


}
